package com.cxf.windex.config;

import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.CachingResourceResolver;
import org.springframework.web.servlet.resource.GzipResourceResolver;
import org.springframework.web.servlet.resource.PathResourceResolver;

/**
 * @author chenxf
 * @date 3/8/2018
 * @description
 */

@Configuration
@EnableCaching
public class MyWebConfigurer  extends WebMvcConfigurerAdapter {
  @Value("${upload.path}")
  private String uploadPath;
  @Value("${upload.url}")
  private String uploadUrl;

  @Autowired
  private CacheManager cacheManager;

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(uploadUrl+"/**").addResourceLocations("file:" + uploadPath + "/").setCacheControl(
        CacheControl.maxAge(30, TimeUnit.DAYS)) .resourceChain(true)
        .addResolver(new CachingResourceResolver(cacheManager, "resourceCache"))
        .addResolver(new GzipResourceResolver())
        .addResolver(new PathResourceResolver());
    super.addResourceHandlers(registry);
  }



}
